package com.sxfq.loansupermarket.backsystem.modular.arrival.controller;

import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.CustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.FirstCustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.ScoreDealInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.service.ICustomerDetailsService;
import com.sxfq.loansupermarket.backsystem.modular.arrival.service.ICustomerService;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.PayServicedInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.TcInfo;
import com.sxfq.loansupermarket.backsystem.modular.doctorLooked.entity.*;
import com.sxfq.loansupermarket.backsystem.modular.doctorLooked.service.IDoctorDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 医生看诊/update
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/customerDetails")
public class CustomerDetailsController extends BaseController {
    @Autowired
    ICustomerDetailsService customerDetailsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    IDoctorDetailsService doctorDetailsService;

    @Autowired
    ICustomerService customerService;

    private String PREFIX = "/tcCenter/consultant/";

    @RequestMapping(value = "/to_openBileLists/{id}/{tabId}")
    public String to_openBileLists(@PathVariable String id, @PathVariable String tabId, Model model) throws Exception {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        CustomerInfo customerInfo = customerDetailsService.selectOneById(id); //当前记录的患者信息
        FirstCustomerInfo firstCustomerInfo = customerDetailsService.firstVisitInformation(customerInfo.getReservationsId()); //首次到店的患者信息
        customerDetailsService.getCustomerInformationsList();
        customerDetailsService.getVipLevelName(customerInfo.getReservationsId());
        customerInfo.setOrthodonticNumber(customerDetailsService.getorthodonticNumber(customerInfo.getReservationsId()));
        customerInfo.setFirstAppointmentPayService(firstCustomerInfo.getAppointmentPayService());
        customerInfo.setFirstArchiving(firstCustomerInfo.getArchiving());
        customerInfo.setFirstArrivalTime(firstCustomerInfo.getArrivalTime());
        customerInfo.setFirstFieldTc(firstCustomerInfo.getFieldTc());
        customerDetailsService.intodayOperation(customerInfo);
        model.addAttribute("occupationList", ShiroKit.getUser().getOccupationInfoList());
        model.addAttribute("refereeShipList", ShiroKit.getUser().getRefereeShipInfoList());
        model.addAttribute("tabId", tabId);
        model.addAttribute("customerInfo", customerInfo);
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        model.addAttribute("auditswitch",redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Auditswitch",0));
        model.addAttribute("roleNameLogin", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"RoleName",0));
        model.addAttribute("dealyesLogin", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Dealyes",0));
        return PREFIX + "customer_Details.html";
    }

    @RequestMapping(value = "/to_openByReservationKwId/{reservationId}/{tabId}")
    public String to_openByReservationKwId(@PathVariable String reservationId, @PathVariable String tabId, Model model) throws Exception {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        CustomerInfo customerInfo = customerDetailsService.firstVisitInformations(reservationId); //首次到店的患者信息
        customerDetailsService.getCustomerInformationsList();
        customerInfo.setOrthodonticNumber(customerDetailsService.getorthodonticNumber(customerInfo.getReservationsId()));
        customerDetailsService.getVipLevelName(reservationId);
        customerInfo.setFirstAppointmentPayService(customerInfo.getAppointmentPayService());
        customerInfo.setFirstArchiving(customerInfo.getArchiving());
        customerInfo.setFirstArrivalTime(customerInfo.getArrivalTime());
        customerInfo.setFirstFieldTc(customerInfo.getFieldTc());
        customerDetailsService.intodayOperation(customerInfo);
        model.addAttribute("occupationList", ShiroKit.getUser().getOccupationInfoList());
        model.addAttribute("refereeShipList", ShiroKit.getUser().getRefereeShipInfoList());
        model.addAttribute("tabId", tabId);
        model.addAttribute("id", customerInfo.getId());
        model.addAttribute("customerInfo", customerInfo);
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        model.addAttribute("auditswitch",redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Auditswitch",0));
        model.addAttribute("roleNameLogin", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"RoleName",0));
        model.addAttribute("dealyesLogin", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Dealyes",0));
        return PREFIX + "customer_Details.html";
    }

    @RequestMapping(value = "/to_openByReservationId/{reservationId}/{tabId}")
    public String to_openByReservationId(@PathVariable String reservationId, @PathVariable String tabId, Model model) throws Exception {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        CustomerInfo customerInfo = customerDetailsService.selectOneByReservationsId(reservationId);
        FirstCustomerInfo firstCustomerInfo = customerDetailsService.firstVisitInformation(reservationId); //首次到店的患者信息
        customerDetailsService.getCustomerInformationsList();
        customerDetailsService.getVipLevelName(reservationId);
        customerInfo.setOrthodonticNumber(customerDetailsService.getorthodonticNumber(customerInfo.getReservationsId()));
        customerInfo.setFirstAppointmentPayService(firstCustomerInfo.getAppointmentPayService());
        customerInfo.setFirstArchiving(firstCustomerInfo.getArchiving());
        customerInfo.setFirstArrivalTime(firstCustomerInfo.getArrivalTime());
        customerInfo.setFirstFieldTc(firstCustomerInfo.getFieldTc());
        customerDetailsService.intodayOperation(customerInfo);
        model.addAttribute("occupationList", ShiroKit.getUser().getOccupationInfoList());
        model.addAttribute("refereeShipList", ShiroKit.getUser().getRefereeShipInfoList());
        model.addAttribute("tabId", tabId);
        model.addAttribute("id", customerInfo.getId());
        model.addAttribute("customerInfo", customerInfo);
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        model.addAttribute("auditswitch",redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Auditswitch",0));
        model.addAttribute("roleNameLogin", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"RoleName",0));
        model.addAttribute("dealyesLogin", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Dealyes",0));
        return PREFIX + "customer_Details.html";
    }

    //患者信息
    @RequestMapping(value = "/patientInformation", method = RequestMethod.POST)
    @ResponseBody
    public CustomerInfo patientInformation(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        CustomerInfo customerInfo = customerDetailsService.selectOneById(id); //当前记录的患者信息
        FirstCustomerInfo firstCustomerInfo = customerDetailsService.firstVisitInformation(customerInfo.getReservationsId()); //首次到店的患者信息
        customerInfo.setOrthodonticNumber(customerDetailsService.getorthodonticNumber(customerInfo.getReservationsId()));
        customerInfo.setFirstAppointmentPayService(firstCustomerInfo.getAppointmentPayService());
        customerInfo.setFirstArchiving(firstCustomerInfo.getArchiving());
        customerInfo.setFirstArrivalTime(firstCustomerInfo.getArrivalTime());
        customerInfo.setFirstFieldTc(firstCustomerInfo.getFieldTc());
        return customerInfo;
    }

    //就诊记录
    @RequestMapping(value = "/visitRecords", method = RequestMethod.POST)
    @ResponseBody
    public List<CustomerInfo> visitRecords(String reservationsId) {
        List<CustomerInfo> customerInfoList = customerDetailsService.visitRecords(reservationsId);
        return customerInfoList;
    }

    //看诊记录
    @RequestMapping(value = "/lookVisitRecord", method = RequestMethod.POST)
    @ResponseBody
    public DoctorRevisitInfo lookVisitRecord(String reservationsId) {
        DoctorRevisitInfo doctorRevisitInfo = doctorDetailsService.lookVisitRecord(reservationsId);
        return doctorRevisitInfo;
    }

    //未结帐记录
    @RequestMapping(value = "/unclosedRecords", method = RequestMethod.POST)
    @ResponseBody
    public List<PayServicedInfo> unclosedRecords(String reservationsId) {
        List<PayServicedInfo> unclosedRecordsList = customerDetailsService.unclosedRecords(reservationsId);
        return unclosedRecordsList;
    }

    //账单记录
    @RequestMapping(value = "/chargingRecord", method = RequestMethod.POST)
    @ResponseBody
    public ArrearageRecordInfo billingRecords(String reservationsId) {
        ArrearageRecordInfo arrearageRecordInfo = doctorDetailsService.billingRecords(reservationsId);
        return arrearageRecordInfo;
    }

    //支付记录
    @RequestMapping(value = "/paymentRecords", method = RequestMethod.POST)
    @ResponseBody
    public ArrearageRecordInfo paymentRecords(String reservationsId) {
        ArrearageRecordInfo arrearageRecordInfo = doctorDetailsService.paymentRecords(reservationsId);
        return arrearageRecordInfo;
    }

    //欠费记录
    @RequestMapping(value = "/arrearageRecord", method = RequestMethod.POST)
    @ResponseBody
    public ArrearageRecordInfo arrearageRecord(String reservationsId) {
        ArrearageRecordInfo arrearageRecordInfo = doctorDetailsService.arrearageRecord(reservationsId);
        String checkoutMethodHtml = customerDetailsService.getCheckoutMethodList();
        List<TcInfo> tcList = customerDetailsService.getTcList();
        arrearageRecordInfo.setTcList(tcList);
        arrearageRecordInfo.setCheckoutMethodHtml(checkoutMethodHtml);
        return arrearageRecordInfo;
    }

    //病历
    @RequestMapping(value = "/medicalRecord", method = RequestMethod.POST)
    @ResponseBody
    public List<CaseInfo> medicalRecord(String reservationsId) {
        List<CaseInfo> caseInfoList = doctorDetailsService.medicalRecord(reservationsId);
        return caseInfoList;
    }

    //会员信息
    @RequestMapping(value = "/memberInformation", method = RequestMethod.POST)
    @ResponseBody
    public ScoreDealInfo memberInformation(String reservationsId) {
        ScoreDealInfo scoreDealInfo = customerDetailsService.memberInformation(reservationsId);
        return scoreDealInfo;
    }
}